<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>计算器</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background-color: #f4f4f9;
    }

   .calculator {
      background-color: #fff;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      text-align: center;
    }

   .calculator h2 {
      margin-bottom: 20px;
      color: #333;
    }

   .calculator input,
   .calculator select {
      width: 100%;
      padding: 10px;
      margin: 5px 0;
      border: 1px solid #ccc;
      border-radius: 4px;
      box-sizing: border-box;
    }

   .calculator button {
      width: 100%;
      padding: 10px;
      margin-top: 15px;
      background-color: #007BFF;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

   .calculator button:hover {
      background-color: #0056b3;
    }

    #result {
      margin-top: 20px;
      font-size: 18px;
      color: #333;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="calculator">
      <h2>带下拉框的计算器</h2>
      <input type="number" v-model="num1" placeholder="输入第一个数字">
      <select v-model="operator">
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
      </select>
      <input type="number" v-model="num2" placeholder="输入第二个数字">
      <button @click="calculate">计算</button>
      <div id="result">{{ result }}</div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        num1: '',
        num2: '',
        operator: '+',
        result: ''
      },
      methods: {
        calculate() {
          const parsedNum1 = parseFloat(this.num1);
          const parsedNum2 = parseFloat(this.num2);

          if (isNaN(parsedNum1) || isNaN(parsedNum2)) {
            this.result = '请输入有效的数字';
            return;
          }

          switch (this.operator) {
            case '+':
              this.result = parsedNum1 + parsedNum2;
              break;
            case '-':
              this.result = parsedNum1 - parsedNum2;
              break;
            case '*':
              this.result = parsedNum1 * parsedNum2;
              break;
            case '/':
              if (parsedNum2 === 0) {
                this.result = '错误：除数不能为零';
              } else {
                this.result = parsedNum1 / parsedNum2;
              }
              break;
            default:
              this.result = '未知运算符';
          }
        }
      }
    });
  </script>
</body>

</html>